package com.oceantrader.sale.biz.staff.service;

import java.util.List;

import org.springframework.stereotype.Component;

import com.oceantrader.root.tools.Dao;
import com.oceantrader.sale.biz.staff.dao.StaffDao;
import com.oceantrader.sale.biz.staff.entity.Staff;
import com.oceantrader.sale.biz.staff.pojo.StaffSearchParam;
import com.xuesinuo.xtool.Np;

import io.vertx.core.Future;
import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class StaffService {
    private final Dao dao;
    private final StaffDao staffDao;

    /** 增 */
    public Future<Long> addStaff(Staff param) {
        var tx = dao.getTransaction();
        param.setCreateDatetime(Np.i(param.getCreateDatetime()).o(System.currentTimeMillis()));
        return staffDao.insert(tx, param)
                .onSuccess(handler -> dao.commit(tx))
                .onFailure(handler -> dao.rollback(tx));
    }

    /** 删 */
    public Future<Integer> delStaff(List<Long> idList) {
        var tx = dao.getTransaction();
        return staffDao.deleteByIdList(tx, idList)
                .onSuccess(handler -> dao.commit(tx))
                .onFailure(handler -> dao.rollback(tx));
    }

    /** 改 */
    public Future<Integer> setStaff(Staff param) {
        var tx = dao.getTransaction();
        return staffDao.updateById(tx, param)
                .onSuccess(handler -> dao.commit(tx))
                .onFailure(handler -> dao.rollback(tx));
    }

    /** 查：排除password */
    public Future<List<Staff>> getStaff(StaffSearchParam param) {
        var tx = dao.getConnection();
        return staffDao.selectList(tx, param)
                .onSuccess(staffList -> {
                    staffList.forEach(staff -> staff.setPassword(null));
                    dao.commit(tx);
                })
                .onFailure(handler -> dao.rollback(tx));
    }

    /** 查询Count */
    public Future<Long> getStaffCount(StaffSearchParam param) {
        var tx = dao.getConnection();
        return staffDao.selectCount(tx, param)
                .onSuccess(handler -> dao.commit(tx))
                .onFailure(handler -> dao.rollback(tx));
    }

    /** 按ID查询：排除password */
    public Future<Staff> getStaffById(Long id) {
        var tx = dao.getConnection();
        return staffDao.selectById(tx, id)
                .onSuccess(staff -> {
                    staff.setPassword(null);
                    dao.commit(tx);
                })
                .onFailure(handler -> dao.rollback(tx));
    }

    /** 按ID查询password */
    public Future<String> getStaffPasswordById(Long id) {
        var tx = dao.getConnection();
        return staffDao.selectById(tx, id)
                .map(staff -> staff.getPassword())
                .onSuccess(password -> dao.commit(tx))
                .onFailure(handler -> dao.rollback(tx));
    }

    /** 按username查询 */
    public Future<Staff> getStaffByUsername(String username) {
        var tx = dao.getConnection();
        return staffDao.selectByUsername(tx, username)
                .onSuccess(handler -> dao.commit(tx))
                .onFailure(handler -> dao.rollback(tx));
    }

}
